package asteroids.model.programs.parsing.statements;
import java.util.ArrayList;
import java.util.List;

public class Sequence extends S{

	private int lastExecute = 0;
	private List<S> statements;
	
	public Sequence(int line, int column, List<S> statements) {
		super(line, column);
		setStatements(statements);
	}
	
	public List<S> getStatements(){
		return this.statements;
	}
	
	private void setStatements(List<S> statements){
		this.statements = statements;
	}
	
	public int getLastExecute(){
		return this.lastExecute;
	}
	
	public void incrementLastExecute(){
		setLastExecute(getLastExecute()+1);
	}
	public void decreaseLastExecute(){
		setLastExecute(getLastExecute()-1);
	}
	public void setLastExecute(int nb){
		this.lastExecute = nb;
	}
	public void restart(){
		setLastExecute(0);
		setNbOfExecutes(0);
	}
	
	
	public void redo(){
		if(this.getExecuted()){
			for(S statement: getStatements()){
				statement.redo();
			}
			restart();			
		}
		super.redo();
	}
	
//	public List<S> getAllStatements(){
//		List<S> statements = new ArrayList<S>();
//		List<S> statementsToReturn = new ArrayList<S>();
//		statements = this.getStatements();
//		for(S statement: statements){
//			if(statement.getClass() == Sequence.class){
//				((Sequence)statement).getAllStatements();
//			}
//			else{
//				statementsToReturn.add(statement);
//			}
//		}
//		return statements;
//	}
	

	private List<S> allStatements = new ArrayList<S>();
	private void setAllStatements(S s){
		if(s.getClass() == Sequence.class){
			Sequence seq = (Sequence)s;
			List<S> seqStatements = seq.getStatements();
			for (S seqStatement : seqStatements) {
				setAllStatements(seqStatement);
			}
		}
		else{
			allStatements.add(s);
		}
	}
	
	public List<S> getAllStatements(){
		allStatements.clear();
		setAllStatements(this);
		return allStatements;
	}
	
	
	
	
	
	@Override
	public void execute(ExecuteManager executeManager) {
			executeManager.execute(this);
	}
	private int nbOfExecutes;
	public int getNbOfExecutes(){
		return this.nbOfExecutes;
	}
	public void incrementNbOfExecutes(){
		nbOfExecutes++;
	}
	public void setNbOfExecutes(int nb){
		this.nbOfExecutes = nb;
	}
}
